package com.xunji.xunji.controller;

import android.util.Log;

import com.qiniu.android.common.Zone;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.xunji.xunji.module.account.controller.UserAccount;

import org.json.JSONObject;

import java.io.File;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Paul on 2017/3/2.
 */

public class QiniuManager {

    private static QiniuManager qiniuManager;

    UploadManager uploadManager;

    public static QiniuManager getInstance() {

        if (qiniuManager == null) {
            qiniuManager = new QiniuManager();
        }
        return qiniuManager;
    }

    public QiniuManager() {

        Configuration config = new Configuration.Builder()
                .chunkSize(256 * 1024)  //分片上传时，每片的大小。 默认256K
                .putThreshhold(512 * 1024)  // 启用分片上传阀值。默认512K
                .connectTimeout(10) // 链接超时。默认10秒
                .responseTimeout(60) // 服务器响应超时。默认60秒
                .zone(Zone.zone0) // 设置区域，指定不同区域的上传域名、备用域名、备用IP。
                .build();

        // 重用uploadManager。一般地，只需要创建一个uploadManager对象
        uploadManager = new UploadManager(config);
    }


    public void upload(File file,UpCompletionHandler handler) {
        File data = file;
        String key =Calendar.getInstance().get(Calendar.DATE)+"/"+Calendar.getInstance().get(Calendar.DAY_OF_MONTH)+"/"+file.getName();
        String token = UserAccount.getInstance().getQiniuToken();
        uploadManager.put(data, key, token,handler, null);

    }
}
